Cornell University ECE4760
USB Host MSC and HID

Pi Pico RP2040

USB flash drive, mouse and keyboard
The USB HID demo code on the Pico C-SDK site works as expected to mount a USB mass store device, produce mouse reports and keyboard reports in the serial window. This project uses the FatFS file system and the Pico USB library to read and write files compatable with a PC. I ported the demo file command line interface application from hathach to our environment. The includes Protothreads and a VGA system running on RP2040.

The result is shown below. The example reads and writes files to a USB flash drive. It also has a few file system commands. The data written is just a simple vector of numbers. The data read from the file system is plotted on the VGA screen.  In this example, 500 samples of a sine wave were computed in Octave on a PC, written to the USB drive, then read on the Pico.  The program to do this is shown in in the serial window below and also linked.

There were several issues that had to be fixed for this to work.

The USB task, tuh_task(), runs in a thread every 8 mSec, which should be fast enough to get mouse events, except perhaps for gaming. It seems that the USB initializer, tuh_init(BOARD_TUH_RHPORT);,  takes some time, so main stalls for 500 mSec after calling tuh_init and before the threader starts. There are four threads running: the USB thread, the usual LED blinky, a serial command thread, and the VGA graphics thread. The USB thread runs on core 0 and just calls the HID task and yields for 8 mSec. The VGA thread runs on core 1 about 30 times/sec to animate the GUI. The GUI event loop in the VGA thread checks for mouse motion, mouse buttons and keypresses, then does whatever action is appropriate based on the mouse position and buttons. The serial thread is, of course, yielding most of the time waiting for the slow human to produce input. Examples of the serial commands are shown below. Implemented file commands are simple versions of their LINUX versions:

None of the commands take options or mulltiple files, except the write command which requires an
integer data range and the integer change from one entry to the next.

Code, Project ZIP -- Octave/Matlab data generator, data file.
The build folder must be added back to the zipped folder.

Example of the interactive commands.

Performance
Timing instrumentation was added along with two more commands in order to get timing information.
Read and write commands now return time in microseconds/entry written/read.
The commands are:
writebin filename length
readbin filename length

modified Code



Copyright Cornell University March 16, 2024